home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / fax / src / port / ultrix / vsyslog.c < prev   
C/C++ Source or Header  |  1994-08-01  |  6KB  |  238 lines

  1. /*
  2.  * Copyright (c) 1983, 1988 Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #if defined(LIBC_SCCS) && !defined(lint)
  35. static char sccsid[] = "@(#)syslog.c    5.34 (Berkeley) 6/26/91";
  36. #endif /* LIBC_SCCS and not lint */
  37.  
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <sys/file.h>
  41. #include <sys/syslog.h>
  42. #include <sys/uio.h>
  43. #include <sys/errno.h>
  44. #include <netdb.h>
  45. #include <string.h>
  46. #if __STDC__
  47. #include <stdarg.h>
  48. #else
  49. #include <varargs.h>
  50. #endif
  51. #include <time.h>
  52. #include <unistd.h>
  53. #include <paths.h>
  54. #include <stdio.h>
  55.  
  56. #define _PATH_LOG "/dev/log"
  57.  
  58. char*     index(const char*, int);
  59.  
  60. static int    LogFile = -1;        /* fd for log */
  61. static int    connected;        /* have done connect */
  62. static int    LogStat = 0;        /* status bits, set by openlog() */
  63. static const char *LogTag = "syslog";    /* string to tag the entry with */
  64. static int    LogFacility = LOG_USER;    /* default facility code */
  65. static int    LogMask = 0xff;        /* mask of priorities to be logged */
  66.  
  67. /*
  68.  * syslog, vsyslog --
  69.  *    print message on log file; output is intended for syslogd(8).
  70.  */
  71. void
  72. #if __STDC__
  73. syslog(int pri, const char *fmt, ...)
  74. #else
  75. syslog(pri, fmt, va_alist)
  76.     int pri;
  77.     char *fmt;
  78.     va_dcl
  79. #endif
  80. {
  81.     va_list ap;
  82.  
  83. #if __STDC__
  84.     va_start(ap, fmt);
  85. #else
  86.     va_start(ap);
  87. #endif
  88.     vsyslog(pri, fmt, ap);
  89.     va_end(ap);
  90. }
  91.  
  92. vsyslog(pri, fmt, ap)
  93.     int pri;
  94.     register const char *fmt;
  95.     va_list ap;
  96. {
  97.     register int cnt;
  98.     register char *p;
  99.     time_t now, time();
  100.     int fd, saved_errno;
  101.     char tbuf[2048], fmt_cpy[1024], *stdp, *ctime();
  102.  
  103.     /* check for invalid bits or no priority set */
  104.     if (!LOG_PRI(pri) || (pri &~ (LOG_PRIMASK|LOG_FACMASK)) ||
  105.         !(LOG_MASK(pri) & LogMask))
  106.         return;
  107.  
  108.     saved_errno = errno;
  109.  
  110.     /* set default facility if none specified */
  111.     if ((pri & LOG_FACMASK) == 0)
  112.         pri |= LogFacility;
  113.  
  114.     /* build the message */
  115.     (void)time(&now);
  116.     (void)sprintf(tbuf, "<%d>%.15s ", pri, ctime(&now) + 4);
  117.     for (p = tbuf; *p; ++p);
  118.     if (LogStat & LOG_PERROR)
  119.         stdp = p;
  120.     if (LogTag) {
  121.         (void)strcpy(p, LogTag);
  122.         for (; *p; ++p);
  123.     }
  124.     if (LogStat & LOG_PID) {
  125.         (void)sprintf(p, "[%d]", getpid());
  126.         for (; *p; ++p);
  127.     }
  128.     if (LogTag) {
  129.         *p++ = ':';
  130.         *p++ = ' ';
  131.     }
  132.  
  133.     /* substitute error message for %m */
  134.     {
  135.         register char ch, *t1, *t2;
  136.         char *strerror();
  137.  
  138.         for (t1 = fmt_cpy; ch = *fmt; ++fmt)
  139.             if (ch == '%' && fmt[1] == 'm') {
  140.                 ++fmt;
  141.                 for (t2 = strerror(saved_errno);
  142.                     *t1 = *t2++; ++t1);
  143.             }
  144.             else
  145.                 *t1++ = ch;
  146.         *t1 = '\0';
  147.     }
  148.  
  149.     (void)vsprintf(p, fmt_cpy, ap);
  150.  
  151.     cnt = strlen(tbuf);
  152.  
  153.     /* output to stderr if requested */
  154.     if (LogStat & LOG_PERROR) {
  155.         struct iovec iov[2];
  156.         register struct iovec *v = iov;
  157.  
  158.         v->iov_base = stdp;
  159.         v->iov_len = cnt - (stdp - tbuf);
  160.         ++v;
  161.         v->iov_base = "\n";
  162.         v->iov_len = 1;
  163.         (void)writev(STDERR_FILENO, iov, 2);
  164.     }
  165.  
  166.     /* get connected, output the message to the local logger */
  167.     if (!connected)
  168.         openlog(LogTag, LogStat | LOG_NDELAY, 0);
  169.     if (send(LogFile, tbuf, cnt, 0) >= 0)
  170.         return;
  171.  
  172.     /* see if should attempt the console */
  173.     if (!(LogStat&LOG_CONS))
  174.         return;
  175.  
  176.     /*
  177.      * Output the message to the console; don't worry about blocking,
  178.      * if console blocks everything will.  Make sure the error reported
  179.      * is the one from the syslogd failure.
  180.      */
  181.     if ((fd = open(_PATH_CONSOLE, O_WRONLY, 0)) >= 0) {
  182.         (void)strcat(tbuf, "\r\n");
  183.         cnt += 2;
  184.         p = index(tbuf, '>') + 1;
  185.         (void)write(fd, p, cnt - (p - tbuf));
  186.         (void)close(fd);
  187.     }
  188. }
  189.  
  190. static struct sockaddr SyslogAddr;    /* AF_UNIX address of local logger */
  191.  
  192. openlog(ident, logstat, logfac)
  193.     const char *ident;
  194.     int logstat, logfac;
  195. {
  196.     if (ident != NULL)
  197.         LogTag = ident;
  198.     LogStat = logstat;
  199.     if (logfac != 0 && (logfac &~ LOG_FACMASK) == 0)
  200.         LogFacility = logfac;
  201.  
  202.     if (LogFile == -1) {
  203.         SyslogAddr.sa_family = AF_UNIX;
  204.         (void)strncpy(SyslogAddr.sa_data, _PATH_LOG,
  205.             sizeof(SyslogAddr.sa_data));
  206.         if (LogStat & LOG_NDELAY) {
  207.             if ((LogFile = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
  208.                 return;
  209.             (void)fcntl(LogFile, F_SETFD, 1);
  210.         }
  211.     }
  212.     if (LogFile != -1 && !connected)
  213.         if (connect(LogFile, &SyslogAddr, sizeof(SyslogAddr)) == -1) {
  214.             (void)close(LogFile);
  215.             LogFile = -1;
  216.         } else
  217.             connected = 1;
  218. }
  219.  
  220. closelog()
  221. {
  222.     (void)close(LogFile);
  223.     LogFile = -1;
  224.     connected = 0;
  225. }
  226.  
  227. /* setlogmask -- set the log mask level */
  228. setlogmask(pmask)
  229.     int pmask;
  230. {
  231.     int omask;
  232.  
  233.     omask = LogMask;
  234.     if (pmask != 0)
  235.         LogMask = pmask;
  236.     return (omask);
  237. }
  238.